source('/home/jc148322/scripts/libraries/cool_functions.r')
library(SDMTools)
climate.dir='/home/jc165798/Climate/CIAS/AWT/250m/baseline.76to05/bioclim/'
wd='/home/jc148322/frog_declines/SDM2/';setwd(wd)
out.dir='/home/jc148322/frog_declines/SDM2/boxplots/'; dir.create(out.dir)

timeline=c('Pre_decline','Post_decline')

species=list.files(paste(wd,timeline[2],'/models/',sep=''))

clim.vars=paste('bioclim_',sprintf('%02i',c(1:19)),sep='')
for (clim.var in clim.vars){ cat(clim.var,'\n')
	tasc=read.asc.gz(paste(climate.dir,clim.var,'.asc.gz',sep=''))
	assign(clim.var,tasc)
}

var.names=c('BIO1 = Annual Mean Temperature',
			'BIO2 = Mean Diurnal Range',
			'BIO3 = Isothermality',
			'BIO4 = Temperature Seasonality ',
			'BIO5 = Max Temperature of Warmest Month',
			'BIO6 = Min Temperature of Coldest Month',
			'BIO7 = Temperature Annual Range',
			'BIO8 = Mean Temperature of Wettest Quarter',
			'BIO9 = Mean Temperature of Driest Quarter',
			'BIO10 = Mean Temperature of Warmest Quarter',
			'BIO11 = Mean Temperature of Coldest Quarter',
			'BIO12 = Annual Precipitation',
			'BIO13 = Precipitation of Wettest Month',
			'BIO14 = Precipitation of Driest Month',
			'BIO15 = Precipitation Seasonality',
			'BIO16 = Precipitation of Wettest Quarter',
			'BIO17 = Precipitation of Driest Quarter',
			'BIO18 = Precipitation of Warmest Quarter',
			'BIO19 = Precipitation of Coldest Quarter')

for (spp in species) {cat(spp,'\n')
	for (tt in timeline) { cat(tt,'\n')
		var.dir=paste(wd, tt, '/models/',spp,'/bioclim.01.02.03.04.05.06.07.08.09.10.11.12.13.14.15.16.17.18.19/',sep='');setwd(var.dir)
		
		occur=read.csv('occur.csv',as.is=TRUE)
		occur=occur[1:3]
		for (clim.var in clim.vars) { cat(clim.var,'\n') #cycle through each of hte climate variables
			occur[,clim.var] = extract.data(cbind(occur$long,occur$lat), get(clim.var)) #append the climate data
		}
		assign(tt,occur)
	}

	setwd(out.dir)
	i=0
	for (clim.var in clim.vars) { cat(clim.var,'\n')
		i=i+1
		tdata=list(Pre_decline[,clim.var],Post_decline[,clim.var])
		names(tdata)=c('Pre-decline','Post-decline')
		png(paste(out.dir,spp,'_',clim.var,'.png',sep=''))
			boxplot(tdata, main=var.names[i])
		dev.off()

	}	
}	

#what is different between where frogs used to be and where they are now?
out.dir='/home/jc148322/frog_declines/SDM2/boxplots_diff/'; dir.create(out.dir)

for (spp in species) {cat(spp,'\n')
	occur=NULL

	for (tt in timeline) { cat(tt,'\n')
		var.dir=paste(wd, tt, '/models/',spp,'/bioclim.01.02.03.04.05.06.07.08.09.10.11.12.13.14.15.16.17.18.19/',sep='');setwd(var.dir)
		threshold=read.csv('output/maxentResults.csv',as.is=T)
		threshold=threshold$Balance.training.omission..predicted.area.and.threshold.value.logistic.threshold
		
		tasc=read.asc.gz(paste('output/ascii/current.76to05.asc.gz', sep=''))
		tasc[which(tasc<=threshold)]=0; tasc[which(tasc>0)]=1
		if (tt==timeline[1]) pre=tasc; if (tt==timeline[2]) post=tasc

		toccur=read.csv('occur.csv',as.is=T)
		if (tt==timeline[1]) occur=toccur else occur=rbind(occur,toccur)
		
		
		
	}
	pre.diff=pre-post
	post.diff=post-pre
	always=post*pre
	
	occur$always=extract.data(cbind(occur$long,occur$lat),always)
	occur$pre=extract.data(cbind(occur$long,occur$lat),pre.diff)
	occur$post=extract.data(cbind(occur$long,occur$lat),post.diff)

	Pre_decline=occur[which(occur$pre==1),]
	Post_decline=occur[which(occur$post==1),]
	Always_suitable=occur[which(occur$always==1),]
	
	setwd(out.dir)
	i=0
	for (clim.var in clim.vars) { cat(clim.var,'\n')
		i=i+1
		tdata=list(Pre_decline[,clim.var],Post_decline[,clim.var],Always_suitable[,clim.var])
		names(tdata)=c('Pre-decline','Post-decline','Always suitable')
		png(paste(out.dir,spp,'_',clim.var,'.png',sep=''))
			boxplot(tdata, main=var.names[i])
		dev.off()

	}	

	
}














































